//
//  CZQResumeSetViewController.m
//  coachmanage
//
//  Created by 众拳科技 on 2018/3/13.
//  Copyright © 2018年 众拳科技. All rights reserved.
//

#import "CZQResumeSetViewController.h"
#import "CZQResumeSetView.h"
#import "CZQLongViewController.h"
#import "ZLPhotoPickerViewController.h"
#import "UIImage+ZLPhotoLib.h"
#import "ZLPhotoPickerBrowserPhoto.h"
#import "MOFSPickerManager.h"

@interface CZQResumeSetViewController ()

@property(nonatomic,weak)CZQResumeSetView *myInfoView;

@property (nonatomic , strong) ZLPhotoPickerBrowserPhoto *photo;

@end

@implementation CZQResumeSetViewController


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title = @"基本资料设置";
    
    [self createSubView];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    
    
    
}

#pragma mark 创建子视图
-(void)createSubView{
    
    [self.myInfoView removeFromSuperview];
    
    CZQResumeSetView *myInfoView = [[CZQResumeSetView alloc] init];
    
    //头像选择
    [myInfoView.headBtn addTarget:self action:@selector(logoSelect:)];
    
    NSString *urlStr =  [NSString stringWithFormat:@"%@%@",self.resumeModel.Avatar ,mid_image];
    urlStr=[urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [myInfoView.headBtn sd_setImageWithURL:[NSURL URLWithString:urlStr] forState:UIControlStateNormal placeholderImage:PlaceHold];
    
    
    //性别
    [myInfoView.sexBtnView.choseBtn addTarget:self action:@selector(showSex)];

    myInfoView.sexBtnView.choseLabel.text = [self.resumeModel.Sex intValue] == 0? @"男" : @"女";
    myInfoView.sexBtnView.choseLabel.textColor = TFColor;
    
    
    //名字
    myInfoView.nameInputView.titleTF.text = self.resumeModel.NickName;
    
    myInfoView.realNameInputView.titleTF.text = self.resumeModel.RealName;

    //地址选择
    myInfoView.addressBtnView.choseLabel.text = self.resumeModel.City;
    
    [myInfoView.addressBtnView.choseBtn addTarget:self action:@selector(addressSelect)];
    
    [myInfoView.sureBtn addTarget:self action:@selector(sureBtnClick)];

    myInfoView.backgroundColor = BackGroundColor;
    
    [self.view addSubview:myInfoView];
    
    //微信
    myInfoView.weChatInputView.titleTF.text = self.resumeModel.WxAccount;
    
    
    
    
    //擅长
    [myInfoView.longBtnView.choseBtn addTarget:self action:@selector(longSelect)];
    
    if (self.resumeModel.FavTags.length > 0) {
        myInfoView.longArr = [self.resumeModel.FavTags componentsSeparatedByString:@","];
    }else
    {
        myInfoView.longArr = nil;
    }
    
    //出生年月
    [myInfoView.timeBtnView.choseBtn addTarget:self action:@selector(showTime)];
    
    //出生年月
    myInfoView.timeBtnView.choseLabel.text = self.resumeModel.BirthDay;
    
    //身高
    myInfoView.heightInputView.titleTF.text = self.resumeModel.Height;
    
    //体重
    myInfoView.weightInputView.titleTF.text = self.resumeModel.Weight;
    
    //工作经历
    [myInfoView.jobBtnView.choseBtn addTarget:self action:@selector(showJob)];
    myInfoView.jobBtnView.choseLabel.text = self.resumeModel.WorkTime;
    
    //学历
    [myInfoView.educationBtnView.choseBtn addTarget:self action:@selector(showEducation)];
    myInfoView.educationBtnView.choseLabel.text = self.resumeModel.Education;
    
    //描述
    myInfoView.talkView.text = self.resumeModel.Description;
    [myInfoView.talkView textViewDidChange:myInfoView.talkView];
    
    [myInfoView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self.view);
    }];
    
    //self.userAccount = [UserAccount account];
    
    self.myInfoView = myInfoView;
}

#pragma Logo图片选择
-(void)logoSelect:(UIButton *)photoSelect{
    
    ZLPhotoPickerViewController *pickerVc = [[ZLPhotoPickerViewController alloc] init];
    
    pickerVc.maxCount = 1;
    
    // Jump AssetsVc
    pickerVc.status = PickerViewShowStatusCameraRoll;
    // Recoder Select Assets
    //    pickerVc.selectPickers = self.assets;
    // Filter: PickerPhotoStatusAllVideoAndPhotos, PickerPhotoStatusVideos, PickerPhotoStatusPhotos.
    
    pickerVc.photoStatus = PickerPhotoStatusPhotos;
    // Desc Show Photos, And Suppor Camera
    pickerVc.isShowCamera = YES;
    pickerVc.topShowPhotoPicker = YES;
    
    // CallBack
    __weak typeof(self)weakSelf = self;
    
    pickerVc.callBack = ^(NSArray<ZLPhotoAssets *> *status){
        
        for (ZLPhotoAssets *assets in status) {
            
            ZLPhotoPickerBrowserPhoto *photo = [ZLPhotoPickerBrowserPhoto photoAnyImageObjWith:assets];
            
            photo.asset = assets;
            
            weakSelf.photo = photo;
            
            //拍照
            if ([photo.asset isKindOfClass:[UIImage class]]) {
                
                [weakSelf.myInfoView.headBtn setImage:(UIImage *)photo.asset forState:UIControlStateNormal];
            }
            else{
                
                [weakSelf.myInfoView.headBtn setImage:[assets thumbImage] forState:UIControlStateNormal];
                
            }
            
            [weakSelf uploadImage];
            
            break;
        }
    };
    
    [pickerVc showPickerVc:self];
}

#pragma mark 上传头像
-(void)uploadImage{
    
    if (!self.photo) {
        
        return;
    }
    
    NSData *imageData;
    
    UIImage *image;
    
    if ([self.photo.asset isKindOfClass:[UIImage class]]) {
        
        image = (UIImage *)self.photo.asset;
    }
    else{
        
        image = [self.photo.asset originImage];
    }
    
    imageData  = UIImageJPEGRepresentation(image,0.1);
    
        [MBProgressHUD showMessage:@"上传中.." toView:self.view];
    
        RAYUploader *upload = [RAYUploader defaultUploader];
    
        [upload uploadWithData:imageData complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
    
            [MBProgressHUD hideHUDForView:self.view];
    
            if (key) {
    
                dispatch_async(dispatch_get_main_queue(), ^{
    
                    NSString *picUrl = [NSString stringWithFormat:@"%@%@",QiNiuBase,key];
    
                    self.resumeModel.Avatar = picUrl;
                });
    
            }
    
        }];
    
}

#pragma mark 显示性别
-(void)showSex{
    
    [self.view endEditing:YES];
    
    __weak typeof(self)weakSelf = self;
    
    [[MOFSPickerManager shareManger] showPickerViewWithDataArray:@[@"男",@"女"] tag:67 title:@"选择性别" cancelTitle:@"取消" commitTitle:@"确定" commitBlock:^(NSString *string) {
        
        weakSelf.myInfoView.sexBtnView.choseLabel.text = string;
        
        weakSelf.myInfoView.sexBtnView.choseLabel.textColor = TFColor;
        
        weakSelf.resumeModel.Sex = [string isEqualToString:@"男"]?@"0":@"1";
        
    } cancelBlock:^{
        
    }];
}




#pragma mark 显示城市
-(void)addressSelect{
    
    __weak typeof(self)weakSelf = self;
    
    [[MOFSPickerManager shareManger] showMOFSAddressPickerWithDefaultAddress:@"" title:@"选择城市" cancelTitle:@"取消" commitTitle:@"确定" commitBlock:^(NSString *address, NSString *zipcode) {
        
        weakSelf.myInfoView.addressBtnView.choseLabel.text = address;
        
        weakSelf.myInfoView.addressBtnView.choseLabel.textColor = TFColor;
        
        weakSelf.resumeModel.City = address;
        
    } cancelBlock:^{
        
        
    }];
}

#pragma mark 显示出生年月
-(void)showTime{
    
    [self.view endEditing:YES];
    
    __weak typeof(self)weakSelf = self;
    
    
    
    [[MOFSPickerManager shareManger] showDatePickerWithTag:68 title:@"选择出生年月"cancelTitle:@"取消" commitTitle:@"确定" datePickerMode:UIDatePickerModeDate commitBlock:^(NSDate *date) {
        
        //处理
        NSDateFormatter *objDateformat1 = [[NSDateFormatter alloc] init];
        [objDateformat1 setDateFormat:@"yyyy-MM-dd"];
         
        NSString *timestamp1 = [objDateformat1 stringFromDate: date];
        
        weakSelf.myInfoView.timeBtnView.choseLabel.text = timestamp1;
        
        weakSelf.myInfoView.timeBtnView.choseLabel.textColor = TFColor;
        
        weakSelf.resumeModel.BirthDay = timestamp1;
        
    } cancelBlock:^{
        
    }];
    
    
}


#pragma mark 显示工作经历
-(void)showJob{
    
    [self.view endEditing:YES];
    
    __weak typeof(self)weakSelf = self;
    
    NSArray *dataArr = @[@"应届",@"1年",@"2年",@"3年",@"4年",@"5年",@"6年",@"7年",@"8年",@"9年",@"10年以上"];
    
    [[MOFSPickerManager shareManger] showPickerViewWithDataArray:dataArr tag:77 title:@"选择工作经验" cancelTitle:@"取消" commitTitle:@"确定" commitBlock:^(NSString *string) {
        
        weakSelf.myInfoView.jobBtnView.choseLabel.text = string;
        
        weakSelf.myInfoView.jobBtnView.choseLabel.textColor = TFColor;
        
        weakSelf.resumeModel.WorkTime = string;
        
    } cancelBlock:^{
        
    }];
    

}

#pragma mark 显示学历
-(void)showEducation{
    
    [self.view endEditing:YES];
    
    __weak typeof(self)weakSelf = self;
    
    [[MOFSPickerManager shareManger] showPickerViewWithDataArray:@[@"专科",@"本科",@"硕士",@"博士"] tag:70 title:@"选择学历" cancelTitle:@"取消" commitTitle:@"确定" commitBlock:^(NSString *string) {
        
        weakSelf.myInfoView.educationBtnView.choseLabel.text = string;
        
        weakSelf.myInfoView.educationBtnView.choseLabel.textColor = TFColor;
        
        weakSelf.resumeModel.Education = string;
        
    } cancelBlock:^{
        
    }];
}


/**
 擅长跳转
 */
-(void)longSelect
{
    
    WS(weakSelf);
    
    CZQLongViewController *longVC = [CZQLongViewController new];
    
    longVC.resumeModel = self.resumeModel;
    
    longVC.saveBlock = ^{
        
        [self createSubView];
        
    };
    
    [self.navigationController pushViewController:longVC animated:YES];
    
}
#pragma mark 保存按钮点击
-(void)sureBtnClick{
    
    if ([self.resumeModel.Avatar isEmptyString]) {
        
        [MBProgressHUD showText:@"请输入头像" toview:self.view];
        
        return;
    }
    
    NSString *nickName = self.myInfoView.nameInputView.titleTF.text;
    
    if ([nickName isEmptyString]) {
        
        [MBProgressHUD showText:@"请输入用户名" toview:self.view];
        
        return;
    }
    
    self.resumeModel.NickName = nickName;
    
    if ([self.myInfoView.realNameInputView.titleTF.text isEmptyString]) {
        
        [MBProgressHUD showText:@"请输入真实姓名" toview:self.view];
        
        return;
    }
    
    self.resumeModel.RealName = self.myInfoView.realNameInputView.titleTF.text;
    
    
    if ([self.resumeModel.Sex isEmptyString]) {
        
        self.resumeModel.Sex = @"0";
    }
    
    if ([self.myInfoView.addressBtnView.choseLabel.text isEmptyString]) {
        
        [MBProgressHUD showText:@"请选择地址" toview:self.view];
        
        return;
    }
    
    if ([self.myInfoView.weChatInputView.titleTF.text isEmptyString]) {
        
        [MBProgressHUD showText:@"请输入微信号" toview:self.view];
        
        return;
    }
    self.resumeModel.WxAccount = self.myInfoView.weChatInputView.titleTF.text;
    
    if ([self.myInfoView.timeBtnView.choseLabel.text isEmptyString]) {
        
        [MBProgressHUD showText:@"请选择出生时间" toview:self.view];
        
        return;
    }
    if ([self.myInfoView.heightInputView.titleTF.text isEmptyString]) {
        
        [MBProgressHUD showText:@"请输入输入身高" toview:self.view];
        
        return;
    }
    self.resumeModel.Height = self.myInfoView.heightInputView.titleTF.text;
    
    if ([self.myInfoView.weightInputView.titleTF.text isEmptyString]) {
        
        [MBProgressHUD showText:@"请输入体重" toview:self.view];
        
        return;
    }
    
    self.resumeModel.Weight = self.myInfoView.weightInputView.titleTF.text;
    
    if ([self.myInfoView.jobBtnView.choseLabel.text isEmptyString]) {
        
        [MBProgressHUD showText:@"请选择工作时间" toview:self.view];
        
        return;
    }
    if ([self.myInfoView.educationBtnView.choseLabel.text isEmptyString]) {
        
        [MBProgressHUD showText:@"请选择学历" toview:self.view];
        
        return;
    }
    
    self.resumeModel.Description = self.myInfoView.talkView.text;
//
    [MBProgressHUD showMessage:@"请求中..." toView:self.view];
    
    
    NSDictionary *params = [self.resumeModel toDictionary];
    
    
    
//        VenueModel *venueModel = [VenueModel venue];
//
//        [params setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"ShopID"] forKey:@"ShopID"];
//
//        [params setObject:self.userAccount.ID forKey:@"ID"];
//
//        [params setValue:self.userAccount.Sex forKey:@"Sex"];
//
//        [params setValue:self.userAccount.Pic forKey:@"Pic"];
//
//        [params setObject:nickName forKey:@"NickName"];
//
//        [params setValue:self.userAccount.Address forKey:@"Address"];
    
        [HTTPTool POSTRaw:Post_Resume_Operate parameters:params
    
                  success:^(id responseObject){
    
                      [MBProgressHUD hideHUDForView:self.view];
                      
                      NSLog(@"%@",responseObject);
    
                      if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
    
                          [MBProgressHUD showText:@"保存成功"];
    
//                          if (self.saveResume) {
//                              self.saveResume();
//                          }
                          
                          //发送刷新一个通知
                          [[NSNotificationCenter defaultCenter] postNotificationName:@"resume" object:nil];
    
                          [self.navigationController popViewControllerAnimated:YES];
                      }
    
                  }
    
                  failure:^(NSError *error){
    
                      [MBProgressHUD hideHUDForView:self.view];
                  }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
